﻿@using FujiyBlog.Core.Caching
@using FujiyBlog.Core.Dto
@using FujiyBlog.Core.EntityFramework
@using FujiyBlog.Core.Extensions
@using Newtonsoft.Json.Linq

@model FujiyBlog.Core.DomainObjects.WidgetSetting
@{
    Layout = "Widget.cshtml";

        JObject settings = JObject.Parse(Model.Settings??"{}");

    IEnumerable<TagWithCount> tagCloud = CacheHelper.FromCacheOrExecute(() => DependencyResolver.Current.GetService<FujiyBlogDatabase>().Tags.GetTagsCloud(settings.Value<int?>("MinimumPosts") ?? 1), "Widget.TagCloud.Tags.GetTagsCloud" + (settings.Value<int?>("MinimumPosts") ?? 1));
    int minTag = tagCloud.Select(x=>x.PostsCount).DefaultIfEmpty().Min();
    int maxTag = tagCloud.Select(x => x.PostsCount).DefaultIfEmpty().Max();
    int distribution = (maxTag - minTag) / 3;
}
<h4>@Html.Raw(settings.Value<string>("Title") ?? "Tag Cloud")</h4>
<div class="tagcloud">


<ul>
    @foreach (var tag in tagCloud)
    {
        string cssClass;
        if (tag.PostsCount == minTag)
        {
            cssClass = "smallest";
        }
        else if (tag.PostsCount == maxTag)
        {
            cssClass = "biggest";
        }
        else if (tag.PostsCount > minTag + 2 * distribution)
        {
            cssClass = "big";
        }
        else if (tag.PostsCount > minTag + distribution)
        {
            cssClass = "medium";
        }
        else
        {
            cssClass = "small";
        }
         
        <li>@Html.ActionLink(tag.Tag.Name, MVC.Post.Tag(tag.Tag.Name, null), new{@class = cssClass})</li>    
    }    
    </ul>
</div>
